package com.eleMall.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eleMall.entity.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import com.eleMall.entity.Category;
import com.eleMall.service.CategoryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 分类(Category)表控制层
 *
 * @author makejava
 * @since 2024-03-12 13:36:52
 */
@Slf4j
@RestController
@RequestMapping("/eleMall/category")
@Api(tags = "分类(Category)表控制层")
public class CategoryController {
    /**
     * 服务对象
     */
    @Resource
    private CategoryService categoryService;

    /**
     * 分页查询
     *
     *
     * @return 查询结果
     */
    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result queryAll(int page,int pageSize) {
        //分页构造器
        Page<Category> pageInfo = new Page<>(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        //添加排序条件，根据id进行排序
        queryWrapper.orderByAsc(Category::getId);

        //分页查询
        categoryService.page(pageInfo,queryWrapper);
        return Result.success(pageInfo);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation("通过主键查询单条数据")
    @GetMapping("{id}")
    public Result<Category> queryById(@ApiParam(value = "id 主键") @PathVariable("id") Long id) {
        Category category = categoryService.getById(id);
        return Result.success(category);
    }

    /**
     * 新增数据
     *
     * @param category 实体
     * @return 新增结果
     */
    @ApiOperation("新增数据")
    @PostMapping
    public Result add(@ApiParam(value = "category 实体") @RequestBody Category category) {
        categoryService.save(category);
        return Result.success("新增成功");
    }

    /**
     * 编辑数据
     *
     * @param category 实体
     * @return 编辑结果
     */
    @ApiOperation("编辑数据")
    @PutMapping
    public Result edit(@ApiParam(value = "category 实体") @RequestBody Category category) {
        categoryService.updateById(category);
        return Result.success("新增成功");
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @ApiOperation("删除数据")
    @DeleteMapping
    public Result deleteById(@ApiParam(value = "id 主键") Long id) {
        log.info("删除用户,id:"+id);
        categoryService.removeById(id);
        return Result.success("删除成功");
    }

}


